﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Windows.Forms.DataVisualization.Charting;
using Analysis.Business;

namespace Analysis
{
    public partial class frmMain : XtraForm
    {
        Dictionary<string, Form> activateForm = new Dictionary<string, Form>();

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = this.Text + "  30 Günlük Kullanım Sürümü";
        }

        private void btnStart_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("1000"))
            {
                frmProgress prg = new frmProgress();
                prg.MdiParent = this;
                activateForm.Add("1000", prg);
                prg.Show();
            }
            else
                SelectPage(activateForm["1000"]);
        }

        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("2000"))
            {
                dbMikro db = new dbMikro();
                frmStandartViewer view = new frmStandartViewer();
                view.MdiParent = this;
                view.Text = "Kesilen Fiyat Farkları";
                view.DataSource = db.KesilenFiyatFarklari();
                activateForm.Add("2000", view);
                view.Show();
            }
            else
                SelectPage(activateForm["2000"]);
        }

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dbMikro db = new dbMikro();
            if (!ContainsForm("3000"))
            {
                frmStandartViewer view = new frmStandartViewer();
                view.MdiParent = this;
                view.Text = "Kesilecek Fiyat Farkları";
                view.DataSource = db.KesilecekFiyatFarklari();
                activateForm.Add("3000", view);
                view.Show();
            }
            else
                SelectPage(activateForm["3000"]);
        }

        private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("4000"))
            {
                dbMikro mikro = new dbMikro();
                DataTable dt = mikro.ChartReport1();

                frmChartReport chart = new frmChartReport();
                chart.Text = "Firmalara Göre Kesilen Fiyat Farkları";
                chart.chartEx1.AddArea("chartArea");

                chart.chartEx1.AddSeries("Fiyat Farkları", "chartArea", "Cari Ünvanı", ChartValueType.String, "Tutar", ChartValueType.Double, SeriesChartType.Column);
                chart.chartEx1.LegendVisible = true;

                chart.chartEx1.DataSource = dt;
                chart.chartEx1.DataBind();
                chart.chartEx1.ChartType = ChartControl.ChartEx.ChartTypes.Style4;
                chart.chartEx1.InitChart();

                chart.dataGrid.DataSource = dt;
                chart.MdiParent = this;
                activateForm.Add("4000", chart);
                chart.Show();
            }
            else
                SelectPage(activateForm["4000"]);


        }

        private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("5000"))
            {
                dbMikro mikro = new dbMikro();
                DataTable dt = mikro.ChartReport2();

                frmChartReport chart = new frmChartReport();
                chart.Text = "Aylara Göre Kesilen Fiyat Farkları";
                chart.chartEx1.AddArea("chartArea");
                chart.comboBoxEdit2.Enabled = false;
                chart.chartEx1.AddSeries("Fiyat Farkları", "chartArea", "Cari Ünvanı", ChartValueType.String, "Tutar", ChartValueType.Double, SeriesChartType.Column);
                chart.chartEx1.Series["Fiyat Farkları"].XAxisType = AxisType.Secondary;
                chart.chartEx1.AddSeries("Aylar", "chartArea", "Ay", ChartValueType.String, "Tutar", ChartValueType.Double, SeriesChartType.SplineArea);
                chart.chartEx1.LegendVisible = true;

                chart.chartEx1.DataSource = dt;
                chart.chartEx1.DataBind();
                chart.chartEx1.ChartType = ChartControl.ChartEx.ChartTypes.Style4;
                chart.chartEx1.InitChart();

                chart.dataGrid.DataSource = dt;
                chart.MdiParent = this;
                activateForm.Add("5000", chart);
                chart.Show();
            }
            else
                SelectPage(activateForm["5000"]);


        }

        private bool ContainsForm(string key)
        {
            bool sonuc = false;
            if (activateForm.ContainsKey(key))
            {
                if (activateForm[key].Visible)
                {
                    sonuc = true;
                }
                else
                {
                    sonuc = false;
                    activateForm.Remove(key);
                }
            }
            return sonuc;
        }

        private void barButtonItem5_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("6000"))
            {
                frmOperasyon opr = new frmOperasyon();
                opr.MdiParent = this;
                activateForm.Add("6000", opr);
                opr.Show();
            }
            else
                SelectPage(activateForm["6000"]);
        }

        private void barButtonItem7_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("7000"))
            {
                frmAyarlar ayar = new frmAyarlar();
                ayar.MdiParent = this;
                activateForm.Add("7000", ayar);
                ayar.Show();
            }
            else
                SelectPage(activateForm["7000"]);

        }

        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            Config.GetConfig.Save();
        }

        void SelectPage(Form frm)
        {
            foreach (DevExpress.XtraTabbedMdi.XtraMdiTabPage item in xtraTabbedMdiManager1.Pages)
            {
                if (item.MdiChild == frm)
                    xtraTabbedMdiManager1.SelectedPage = item;
            }
        }

        private void barButtonItem6_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("Program Hakkında bilgi,istek ve önerileriniz için ,\r\n");
            sb.Append("tr.freelancer@gmail.com adresine mail göndermenizi rica ederiz.");
            MessageBox.Show(sb.ToString());
        }

        private void barButtonItem8_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (!ContainsForm("1001"))
            {
                frmFarkHesap2 prg = new frmFarkHesap2();
                prg.MdiParent = this;
                activateForm.Add("1001", prg);
                prg.Show();
            }
            else
                SelectPage(activateForm["1001"]);
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            dbMikro db = new dbMikro();
            if (!ContainsForm("8000"))
            {
                frmStandartViewer view = new frmStandartViewer();
                view.MdiParent = this;
                view.Text = "Karşılaştırma Raporu";
                frmTarih trh = new frmTarih();
                if (trh.ShowDialog() == DialogResult.OK)
                {
                    view.DataSource = db.KontrolRaporu(trh.Baslangic, trh.Bitis);
                    activateForm.Add("8000", view);
                    view.Show();
                }
            }
            else
                SelectPage(activateForm["8000"]);
        }
    }
}
